package com.supplychainapi.controller;

import com.alibaba.fastjson.JSON;
import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.ApiVersion;
import com.supplychainapi.common.BizCode;
import com.supplychainapi.common.RestResponse;
import com.supplychainapi.domain.HQPurchaseParam;
import com.supplychainapi.domain.LoginCache;
import com.supplychainapi.domain.Tbpurchase;
import com.supplychainapi.model.ScmTbmaterial;
import com.supplychainapi.model.ScmTbpurchase;
import com.supplychainapi.model.ScmTbsupplier;
import com.supplychainapi.param.PurchaseParam;
import com.supplychainapi.service.IScmTbpurchaseService;
import com.supplychainapi.service.IScmTbsupplierService;
import com.supplychainapi.service.TbMaterialService;
import com.supplychainapi.utils.PathUitls;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 采购订单
 *
 * @author:xiezhiwei
 * @email:xiezhiwei@niwodai.net
 * @date:2017-11-27
 */

@RequestMapping(PathUitls.ROOT_PHONE + PathUitls.ROOT_VERSION + PathUitls.ROOT_PURCHASE)
@RestController
@Api(value = "PurchaseController", description = "手机版采购订单")
public class PurchaseController extends BaseController {

    private Logger logger = LogManager.getLogger(PurchaseController.class);

    @Autowired
    private IScmTbpurchaseService purchaseService;
    
	@Autowired
	private IScmTbsupplierService scmTbsupplierService;


    @Autowired
    private TbMaterialService materialService;



    @ApiVersion(1)
    @ApiOperation("采购订单列表信息")
    @RequestMapping(value = "/getPurchaseListInfo", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "fiBillStatus", value = "采购订单状态(0未提交/1已提交/9已收货);swagger", required = true, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "keyWords", value = "关键字;swagger", required = false, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fiBillKind", value = "订单类型;swagger", required = false, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fsArrivalStartDate", value = "到货日期开始时间;swagger", required = false, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fsArrivalEndDate", value = "到货日期结束时间;swagger", required = false, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fsBillStartDate", value = "单据日期开始时间;swagger", required = false, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fsBillEndDate", value = "单据日期结束时间;swagger", required = false, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fsStaffId", value = "制单人;swagger", required = false, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fsCreateUserId", value = "提交人;swagger", required = false, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "page", value = "页码;swagger", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "显示条数;swagger", required = true, dataType = "Integer", defaultValue = "20"),
    })
    public RestResponse<List<ScmTbpurchase>> getPurchaseListInfo(@ApiIgnore LoginCache loginCache, @ApiIgnore @RequestBody PurchaseParam purchaseParam) {
        logger.info("采购订单列表信息 purchaseParam={}",JSON.toJSON(purchaseParam));
        List<ScmTbpurchase> suppliersList = null;
        try {
            purchaseParam.setFsShopGUID(loginCache.getFsShopGUID());
            suppliersList = purchaseService.getPurchaseListInfo(purchaseParam);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
        }
        return RestResponse.success(suppliersList);
    }


    @ApiVersion(1)
    @ApiOperation("采购详情")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public RestResponse<ScmTbpurchase> detail(@ApiIgnore LoginCache loginCache, @ApiParam(value = "采购订单id", required = true) @RequestParam String fsPurchaseGUID) throws ServiceException {
        return RestResponse.success(purchaseService.selectById(fsPurchaseGUID));
    }



    
    @ApiVersion(1)
    @ApiOperation("总部进货供应商")
    @RequestMapping(value = "/getHQSupplierInfo", method = RequestMethod.GET)
    public RestResponse<List<ScmTbsupplier>> getHQSupplierInfo(@ApiIgnore LoginCache loginCache) {
        List<ScmTbsupplier> supplierInfo = null;
        try {
            supplierInfo = scmTbsupplierService.getHeadquartersSupplierInfo(loginCache.getFsShopGUID());
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
        }
        return RestResponse.success(supplierInfo);
    }
    
    /*
    @ApiVersion(1)
    @ApiOperation("单个采购订单")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RestResponse add(@ApiIgnore LoginCache loginCache, @RequestBody Tbpurchase purchase) throws ServiceException {

        logger.info("purchase={}", JSON.toJSON(purchase));
        if (purchase == null) {

    		return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "参数不能为空");
    	}
        ScmTbpurchase tbpurchase = new ScmTbpurchase();
        BeanUtils.copyProperties(purchase, tbpurchase);
        tbpurchase.setFsshopguid(loginCache.getFsShopGUID());
        tbpurchase.setFsshopname(loginCache.getFsShopName());
        tbpurchase.setFscreateuserid(loginCache.getFsUserId());
        tbpurchase.setFscreateusername(loginCache.getFsUserName());
        tbpurchase.setFsupdateuserid(loginCache.getFsUserId());
        tbpurchase.setFsupdateusername(loginCache.getFsUserName());
        return RestResponse.success(purchaseService.addHeadquarters(tbpurchase));
    }*/
    
    @ApiVersion(1)
    @ApiOperation("新增总部采购订单")
    @RequestMapping(value = "/addHeadquarters", method = RequestMethod.POST)
    public RestResponse addHeadquarters(@ApiIgnore LoginCache loginCache, @RequestBody HQPurchaseParam purchase) throws ServiceException {
        logger.info("purchase={}", JSON.toJSON(purchase));
        if (purchase == null) {

    		return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "参数不能为空");
    	}
        return RestResponse.success(purchaseService.addHeadquarters(loginCache,purchase));
    }


    @ApiVersion(1)
    @ApiOperation(value = "根据一级分类获取所有物料", notes = "")
    @RequestMapping(value = "/getMaterialByClass", method = RequestMethod.GET)
    public RestResponse getMaterialByClass(@ApiIgnore LoginCache loginCache, @ApiParam("分页页码从1开始") @RequestParam Integer page, @ApiParam("每页条数") @RequestParam Integer size, @ApiParam("分类代码") @RequestParam String fsNodeCode, @ApiParam(value = "平台供应商guid,不传默认登录者的shopid", required = false) @RequestParam(required = false) String shopGuid) throws ServiceException {
        String shopId = loginCache.getFsShopGUID();
        if (StringUtils.isNotBlank(shopGuid))
            shopId = shopGuid;
        return materialService.selectByClass(shopId, fsNodeCode, page, size);

    }

    @ApiVersion(1)
    @ApiOperation(value = "根据二级分类获取所有物料", notes = "")
    @RequestMapping(value = "/getMaterialByTwoClass", method = RequestMethod.GET)
    public RestResponse getMaterialByTwoClass(@ApiIgnore LoginCache loginCache, @ApiParam("分类代码") @RequestParam String fsNodeCode, @ApiParam(value = "平台供应商guid,不传默认登录者的shopid", required = false) @RequestParam(required = false) String shopGuid) throws ServiceException {
        String shopId = loginCache.getFsShopGUID();
        if (StringUtils.isNotBlank(shopGuid))
            shopId = shopGuid;
        return materialService.selectByTwoClass(shopGuid, fsNodeCode);
    }


    @ApiVersion(1)
    @ApiOperation(value = "获取所有物料不区分分类", notes = "")
    @RequestMapping(value = "/getAllMaterial", method = RequestMethod.GET)
    public RestResponse<List<ScmTbmaterial>> getAllMaterial(@ApiIgnore LoginCache loginCache, @ApiParam("页码") @RequestParam Integer page, @ApiParam("每页条数") @RequestParam Integer size, @ApiParam(value = "平台供应商guid吗,不传默认登录者的shopid", required = false) @RequestParam(required = false) String shopGuid) throws ServiceException {
        String shopId = loginCache.getFsShopGUID();
        if (StringUtils.isNotBlank(shopGuid))
            shopId = shopGuid;
        return materialService.getAllMaterial(shopId, page, size);

    }


}
